package com.cskaoyan.servlet;

import com.cskaoyan.bean.vo.GoodsCreateVo;
import com.cskaoyan.service.GoodsService;
import com.cskaoyan.service.Impls.GoodsServiceImpl;
import com.github.pagehelper.PageHelper;
import com.cskaoyan.bean.PageData;
import com.cskaoyan.bean.PageParameter;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.common.CommonServlet;
import com.cskaoyan.mapper.MarketGoodsMapper;
import com.cskaoyan.model.MarketGoods;
import com.cskaoyan.model.MarketGoodsExample;
import com.cskaoyan.util.JacksonUtil;
import com.cskaoyan.util.MyBatisUtil;
import lombok.SneakyThrows;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/admin/goods/*")
public class AdminGoodsServlet extends CommonServlet {
    GoodsService goodsService = new GoodsServiceImpl();
    @SneakyThrows
    public void list(HttpServletRequest request, HttpServletResponse response){
        response.setContentType("application/json;charset=utf-8");

        PageParameter pageParameter = PageParameter.init(request.getParameterMap());

        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketGoodsMapper goodsMapper = sqlSession.getMapper(MarketGoodsMapper.class);
        // 做查询，传入page,limit,sort,order,offset
        // 要考虑分页，还要考虑排序 → 分页插件 → 分页相关的问题
        // 在我们执行查询的基础上 额外增加分页的语句
        // select * from market_goods
        // 假设我们处于第三页，每页20条数据 → page=3&limit=20

        // select * from market_goods limit 40,20
        // preparing: select * from market_goods limit ?,?
        // parameter: 40(Integer), 20(Integer)
        // 分页插件，帮我们构造预编译SQL语句中的分页部分
        // 开启分页，传入page和limit → 执行查询之前开启分页
        PageHelper.startPage(pageParameter.getPage(), pageParameter.getLimit());
        // 可以使用example来改造
        //List<MarketGoods> goodsList = goodsMapper.selectAll();
        MarketGoodsExample example = new MarketGoodsExample();
        example.setOrderByClause(pageParameter.getSort() + " " + pageParameter.getOrder());
        // List<Criterion>           Criteria调用and方法就是向这个List中添加条件
//        MarketGoodsExample.Criteria criteria = example.createCriteria();
//        criteria.andGoodsSnEqualTo("1006013");
//        criteria.andNameLike("%蚕丝%");
//        criteria.andCategoryIdEqualTo(1008008);

        List<MarketGoods> goodsList = goodsMapper.selectByExample(example);

        PageData data = PageData.list(goodsList);


        sqlSession.close();
        BaseRespVo baseRespVo = BaseRespVo.ok(data);

        String jsonStr = JacksonUtil.write(baseRespVo);
        response.getWriter().println(jsonStr);
    }
    public void create(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 接收的请求参数中文乱码问题解决
        response.setContentType("application/json;charset=utf-8");
        // 参数接收
        String jsonStr = request.getReader().readLine();
        // JsonStr转换成对应的对象，封装对象格式
        GoodsCreateVo goodsCreateVo = JacksonUtil.read(jsonStr, GoodsCreateVo.class);
        // 业务处理 → service，将传入的请求体中的数据转换为所封装的对象的格式，再插入数据库
        goodsService.createGoods(goodsCreateVo);

        // 结果响应
        BaseRespVo baseRespVo = BaseRespVo.ok(null);//封装响应体
        response.getWriter().println(JacksonUtil.write(baseRespVo));

    }
}
